package com.android.clinic.doctors.app.glide;

import android.content.Context;

import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.Registry;
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool;
import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory;
import com.bumptech.glide.load.engine.cache.LruResourceCache;
import com.bumptech.glide.module.AppGlideModule;

import java.io.InputStream;

/**
 * Created by cheng on 2018/6/19.
 */
@GlideModule
public class CustomAppGlideModule extends AppGlideModule {

    /**
     * 通过GlideBuilder设置默认的结构(Engine,BitmapPool ,ArrayPool,MemoryCache等等).
     */
    @Override
    public void applyOptions(Context context, GlideBuilder builder) {

        //重新设置内存限制
        builder.setMemoryCache(new LruResourceCache(10 * 1024 * 1024));
        builder.setBitmapPool(new LruBitmapPool(10 * 1024 * 1024));
        //InternalCache构建的缓存是在应用的内部储存，而ExternalCache则是在外部储存。
        builder.setDiskCache(new InternalCacheDiskCacheFactory(context, InternalCacheDiskCacheFactory.DEFAULT_DISK_CACHE_DIR, 100 * 1024 * 1024));
    }

    /**
     * 为App注册一个自定义的String类型的BaseGlideUrlLoader
     */
    @Override
    public void registerComponents(Context context, Registry registry) {

        registry.append(String.class, InputStream.class, new CustomBaseGlideUrlLoader.Factory());
    }

    /**
     * 清单解析的开启
     *
     * 这里不开启，避免添加相同的modules两次
     */
    @Override
    public boolean isManifestParsingEnabled() {
        return false;
    }
}
